package com.controller;


import com.Utils.DateUtil;
import com.dao.HolidayMapper;
import com.dao.RewardDao;
import com.dao.SalaryMapper;
import com.dao.UserDao;
import com.pojo.Salary;
import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.List;

/**
 * 薪资控制器
 * @author fzp
 */
@Controller
public class SalaryController {

    @Autowired
    SalaryMapper salaryMapper;

    @Autowired
    HolidayMapper holidayMapper;

    @Autowired
    UserDao userDao;

    @Autowired
    RewardDao rewardDao;

    @RequestMapping("salary/salaryIndex")
    public String getAllSalary(Model model){
        model.addAttribute("salarylist",salaryMapper.getAllSalary());
        return "salary/salaryIndex";
    }
    @RequestMapping("salary/salaryDetails")
    public String holidaydetails(Model model,int s_id){
        Salary salary=salaryMapper.getSalaryById(s_id);
        model.addAttribute("salary",salary);
        return "salary/salaryDetails";

    }
    @RequestMapping("salary/mysalarys")
    public String mysalary(Model model,Integer u_id){
        List<Salary> salaryList=salaryMapper.getAllSalaryByUid(u_id);
        Double increaseSalary=rewardDao.increaseSalary(u_id);
        if (increaseSalary==null){
             increaseSalary= Double.valueOf(0);
        }
        Double reduceSalary=rewardDao.reduceSalary(u_id);
        if (reduceSalary==null){
            reduceSalary= Double.valueOf(0);
        }
        Double sumreward= Double.valueOf(0);
        if (increaseSalary!=null&&reduceSalary!=null) {
             sumreward=increaseSalary-reduceSalary;
        }
        if (salaryList.size()>0){
        model.addAttribute("salarylist",salaryMapper.getAllSalaryByUid(u_id));
        model.addAttribute("sumdays",holidayMapper.getSumdays(u_id));
        model.addAttribute("sumreward",sumreward);
        model.addAttribute("sumsalary",salaryMapper.getSumsalary(u_id));
        model.addAttribute("sumtotal",salaryMapper.getSumtotal(u_id));
        }else {
            model.addAttribute("msg","你还没有薪资单");
        }
        return "salary/mysalary";
    }
    @RequestMapping("salary/addSalary")
    public String addSalary(Model model,int u_id){
        User user=userDao.getOneById(u_id);
        Salary salary=new Salary(u_id,user.getU_nickname(), DateUtil.getCurrentTime(),user.getD_id(),user.getU_salary(),0,0,user.getU_salary());
        int result=salaryMapper.addSalary(salary);
        if (result>0){
            model.addAttribute("msg1","成功");
        }
        else{
            model.addAttribute("msg1","失败");
        }
        return "salary/salaryIndex";
    }
    @RequestMapping("salary/delSalary")
    public String delSalary(Model model,int s_id){
        int result=salaryMapper.delSalary(s_id);
        if (result>0){
            model.addAttribute("msg","成功");
        }else {
            model.addAttribute("msg","失败");
        }
        return "salary/salaryIndex";
    }
    @RequestMapping("salary/updateSalary")
    public int updateSalary(Salary salary){
        return salaryMapper.updateSalary(salary);
    }
    @RequestMapping("salary/getSalaryById")
    public Salary getSalaryById(int id){
        return salaryMapper.getSalaryById(id);
    }

}
